package com.itboy.mall.ums.memberlevel.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.itboy.common.utils.PageUtils;
import com.itboy.common.core.page.PagePlus;
import com.itboy.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.itboy.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itboy.mall.ums.memberlevel.bo.MallMemberLevelAddBo;
import com.itboy.mall.ums.memberlevel.bo.MallMemberLevelQueryBo;
import com.itboy.mall.ums.memberlevel.bo.MallMemberLevelEditBo;
import com.itboy.mall.ums.memberlevel.domain.MallMemberLevel;
import com.itboy.mall.ums.memberlevel.mapper.MallMemberLevelMapper;
import com.itboy.mall.ums.memberlevel.vo.MallMemberLevelVo;
import com.itboy.mall.ums.memberlevel.service.IMallMemberLevelService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 会员等级Service业务层处理
 *
 * @author itboy
 * @date 2021-08-07
 */
@Service
public class MallMemberLevelServiceImpl extends ServicePlusImpl<MallMemberLevelMapper, MallMemberLevel> implements IMallMemberLevelService {

    @Override
    public MallMemberLevelVo queryById(Long id){
        return getVoById(id, MallMemberLevelVo.class);
    }

    @Override
    public TableDataInfo<MallMemberLevelVo> queryPageList(MallMemberLevelQueryBo bo) {
        PagePlus<MallMemberLevel, MallMemberLevelVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), MallMemberLevelVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<MallMemberLevelVo> queryList(MallMemberLevelQueryBo bo) {
        return listVo(buildQueryWrapper(bo), MallMemberLevelVo.class);
    }

    private LambdaQueryWrapper<MallMemberLevel> buildQueryWrapper(MallMemberLevelQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MallMemberLevel> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getName()), MallMemberLevel::getName, bo.getName());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(MallMemberLevelAddBo bo) {
        MallMemberLevel add = BeanUtil.toBean(bo, MallMemberLevel.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(MallMemberLevelEditBo bo) {
        MallMemberLevel update = BeanUtil.toBean(bo, MallMemberLevel.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(MallMemberLevel entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
